#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'单元测试'

__author__ = '许文杰'

import unittest

class Dict(dict):
    def __init__(self, **kw):
        super().__init__(**kw)
    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'Dict' object has no attribute '%s'"  % key)

    def __setattr__(self, key, value):
        self[key] = value

class TestDict(unittest.TestCase):

    def setUp(self):
        print('set up')

    def tearDown(self):
        print('tear down')

    def test_init(self):
        d = Dict(a = 1, b = 'test')
        self.assertEqual(d.a, 1)
        self.assertEqual(d.b, 'test')
        self.assertTrue((isinstance(d, dict)))

    def test_key(self):
        d = Dict()
        d['key'] = 'value'
        self.assertEqual(d.key, 'value')

    def test_attr(self):
        d = Dict()
        d.key = 'value'
        self.assertTrue('key' in d)
        self.assertEqual(d['key'], 'value')

    def test_keyError(self):
        d = Dict()
        with self.assertRaises(KeyError):
            value = d['empty']

    def test_attrError(self):
        d = Dict()
        with self.assertRaises(AttributeError):
            value = d.empty


if __name__ == '__main__':

    import doctest

    doctest.testmod()

    unittest.main()
